using System;
using System.IO;
using System.Web.Hosting;

namespace OpenWaves.Web.ImageTransformations
{
    public class VirtualFileWrapper : IVirtualFile
    {
        private readonly RelativeUrl url;
        public Url Url
        {
            get { return this.url; }
        }

        private readonly VirtualFile file;
        protected VirtualFile File
        {
            get { return this.file; }
        }

        private readonly VirtualPathProvider virtualPathProvider;
        protected VirtualPathProvider VirtualPathProvider
        {
            get { return virtualPathProvider; }
        }

        public virtual string Hash
        {
            get { return this.GetFileHashFromVpp(this.file.VirtualPath); }
        }

        public virtual Stream Open()
        {
            return this.file.Open();
        }

        public VirtualFileWrapper(RelativeUrl url, VirtualFile file, VirtualPathProvider virtualPathProvider)
        {
            this.url = url;
            this.file = file;
            this.virtualPathProvider = virtualPathProvider;
        }

        private string GetFileHashFromVpp(string virtualPath)
        {
            return this.virtualPathProvider.GetFileHash(
                virtualPath, 
                new[] {virtualPath});
        }
    }
}